// pages/apply/apply.js
//获取应用实例
const app = getApp()

function scodeItem(min, max) {
  var array = [];
  for (var i = min; i <= max; i++) {
    if (i <= 0) {
      array.push(i);
    } else {
      array.push(i);
    }
  }
  return array;
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //服务器图片的路径片段
    imgUrl:app.globalData.iconImgUrl,
    backImg: app.globalData.iconImgUrl+"arrow-left.svg",
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    backIcon: true,
    title: "发布挑战",
    upImgVisible: true,
    scodeItemVisible: true,
    otherVisible: false,
    formeImg: [],
    scodeItem: [
      "抽烟",
      "迟到5分钟",
      "加班10分钟",
      "参加大扫除",
      "协助店长"
    ],
    scodeCount: [
      "-3", "-5", "+10", "+8", "+6"
    ],
    array: [
      "固定加扣分项选择",
      "其它加分项选择",
      '现金'
    ],
    pickIndex: 0,
    addScode: scodeItem(1, 20),
    addScodeIndex: 0,
    addScodeVal: '+10',
    nameIndex: 0,
    modalVisible: false,
    personArray: scodeItem(1, 20),
    personIndex: 0,
    cash: '',
    content: '',
    value: [0, 0],
  },
  showModal() {
    this.setData({
      modalVisible: true
    })
  },
  hideModal() {
    this.setData({
      modalVisible: false
    })
  },
  cashChange(e) {
    var val = e.detail.value
    this.setData({
      cash: val
    })
  },
  // 填写加分事项
  otherChange(e) {
    var val = e.detail.value
    this.setData({
      otherVal: val
    })
  },
  // 工作内容
  contentChange(e) {
    var val = e.detail.value
    this.setData({
      content: val
    })
  },
  toApply() {
    // this.showModal()
    var addtions, that = this,
      markType = this.data.array[this.data.pickIndex]
    if (markType === '固定加扣分项选择') {
      addtions = {
        markType: 'regular',
        markName: this.data.scodeItem[this.data.value[0]],
        mark: this.data.scodeCount[this.data.value[1]] * 1
      }
    } else if (markType === '其它加分项选择') {
      addtions = {
        markType: 'other',
        markName: this.data.otherVal,
        mark: this.data.addScodeVal * 1
      }
    } else {
      addtions = {
        markType: 'cash',
        markName: '现金',
        mark: this.data.cash * 1
      }
    }
    const data = {
      "userCode": wx.getStorageSync('userCode'),
      "content": this.data.content,
      "teamNum": this.data.personArray[this.data.personIndex] * 1,
      "markType": markType,
      "imgArr": this.data.formeImg,
      ...addtions
    }
    console.log(data)
    wx.request({
      url: app.globalData.baseUrl + 'challenge/issueChallenge.do',
      data: data,
      method: 'post',
      success(res) {
        console.log('提交挑战成功', res)
        if (res.data.rstype) {
          that.showModal()
        } else {
          wx.showToast({
            title: res.data.rsmsg,
            image: '/assets/images/icon/error.png'
          })
        }

      }
    })
  },
  PickerPerNum(e) {
    this.setData({
      personIndex: e.detail.value
    })
  },
  bindChange(e) {
    const val = e.detail.value
    const oval = this.data.value
    if (val[0] === oval[0]) {
      this.setData({
        value: [val[1], val[1]]
      })
    } else {
      this.setData({
        value: [val[0], val[0]]
      })
    }

  },
  bindPickerChange(e) {
    const val = e.detail.value
    // console.log(val)
    if (val == 0) {
      this.setData({
        pickIndex: val,
        scodeItemVisible: true,
        otherVisible: false,
        cashVisible: false
      })
    } else if (val == 1) {
      console.log(val)
      this.setData({
        pickIndex: val,
        scodeItemVisible: false,
        otherVisible: true,
        cashVisible: false
      })
    } else {
      console.log(val)
      this.setData({
        pickIndex: val,
        scodeItemVisible: false,
        otherVisible: false,
        cashVisible: true
      })
    }
  },
  scodeChange(e) {
    const val = e.detail.value;
    this.setData({
      addScodeVal: this.data.addScode[val]
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  takePhoto() {
    var that = this;

    wx.chooseImage({
      // count: 3, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        // success
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: app.globalData.baseUrl + 'application/fileUpload.do', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: "file",
          header: {
            "Content-Type": "multipart/form-data"
          },
          success: function (res) {
            var data = res.data
            var item1 = that.data.formeImg[0],
              item2 = that.data.formeImg[1],
              item3 = that.data.formeImg[2],
              imgArr = []
            // console.log(res, item2)
            if (item1 == null) {
              imgArr.push(app.globalData.serverUrl + res.data.split('"')[1])
              that.setData({
                formeImg: imgArr
              })
            }
            // console.log(item2, item2 == null)
            if (item1 != null && item2 == null) {
              that.setData({
                ['formeImg[1]']: app.globalData.serverUrl + res.data.split('"')[1]
              })
            }
            if (item1 != null && item2 != null && item3 == null) {
              that.setData({
                ['formeImg[2]']: app.globalData.serverUrl + res.data.split('"')[1],
                upImgVisible: false
              })
            }

            //do something
          },
          error(err) {
            console.log(err)
          }
        })
      },
      fail: function (errow) {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getPluses()
  },
  // 获取加分项列表
  getPluses() {
    const data = {
      "projectType": 6,
      "integralType": 2
    }
    const that = this
    // console.log(data, this.data.current)
    wx.request({
      url: app.globalData.baseUrl + 'points/getPointsList.do',
      method: 'post',
      data: data,
      success(res) {
        console.log(res)
        const data = res.data.pointsList;
        var scodeItem = [], scodeCount = []

        data.forEach(item => {
          scodeItem.push(item.projectDesc)
          scodeCount.push(item.score)
        })
        that.setData({
          scodeItem: scodeItem,
          scodeCount: scodeCount
        })

      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})